package com.contract.spirit.ui.main.first

import android.annotation.SuppressLint
import android.os.Bundle
import com.contract.commonlib.base.BaseFragment
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.bean.response.MainPageResponse
import com.contract.spirit.databinding.FrgFirstpageRankBinding
import com.contract.spirit.manage.EmptyViewManger
import com.contract.spirit.ui.main.first.adapter.FirstPageRankAdapter

/**
 * @author: witness
 * created: 2021/9/17
 * desc:
 */
class FirstPageRankFrg : BaseFragment<FrgFirstpageRankBinding, MainFirstViewModel>() {
    private lateinit var mainpageResponse: MainPageResponse
    lateinit var rankType: String;
    private val rankAdapter by lazy {
        FirstPageRankAdapter()
    }

    companion object {
        fun newsInstance(
            rankType: String?,
            mainPageResponse: MainPageResponse?
        ): FirstPageRankFrg? {
            val taskListFragment = FirstPageRankFrg()
            val bundle = Bundle()
            bundle.putString("rankType", rankType)
            bundle.putSerializable("data", mainPageResponse)
            taskListFragment.arguments = bundle
            return taskListFragment
        }
    }

    override fun setContentView(): Int {
        return R.layout.frg_firstpage_rank
    }

    override fun initData() {
        arguments?.apply {
            mainpageResponse = getSerializable("data") as MainPageResponse
            rankType = getString("rankType").toString()
        }
        val rvRank = mBinding.rvRank;
        RecycleviewLayoutManger.setVerticalManager(rvRank, mContext)
        rankAdapter.setEmptyView(EmptyViewManger.getEmptyView(mContext, "暂无排名", R.dimen.dp_50))
        rankAdapter.setRankType(rankType)
        rvRank.adapter = rankAdapter
        setData()
    }

    @SuppressLint("SetTextI18n")
    private fun setRankDesc() {
        when (rankType) {
            FirstPageRankAdapter.INVITE -> mBinding.tvRankTypeDesc.text = "邀请人数"
            FirstPageRankAdapter.COMPENSATED -> mBinding.tvRankTypeDesc.text = "赔付金额(USDT)"
            FirstPageRankAdapter.OPEN -> mBinding.tvRankTypeDesc.text = "开仓笔数"
        }
    }

    fun refreshRankInfo(response: MainPageResponse?) {
        this.mainpageResponse = response!!
        setData()
    }

    fun setData() {
        setRankDesc()
        when (rankType) {
            FirstPageRankAdapter.INVITE ->
                //邀请排行版
                rankAdapter.setList(mainpageResponse.inviteTopList)
            FirstPageRankAdapter.COMPENSATED ->
                //获赔排行版
                rankAdapter.setList(mainpageResponse.gainAmtTopList)
            FirstPageRankAdapter.OPEN ->
                //开仓排行版
                rankAdapter.setList(mainpageResponse.openOrderTopList)
        }
    }
}